package com.codingvip.mybatis.model;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

public class MybatisTest {
    private SqlSessionFactory sqlSessionFactory;


    @Before
    public void prepare() throws IOException {
        String resource = "mybatis-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resource);
        sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(inputStream);
        inputStream.close();
    }

    @Test
    public void testMyBatis() throws IOException {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            CustomerDao articleDao = session.getMapper(CustomerDao.class);
            List<Customer> articles = articleDao.
                    findByAuthorAndCreateTime("songtao", "2018-04-01");
            System.out.println(articles == null ? 0 : articles.size());


        } finally {
            session.commit();
            session.close();
        }
    }
}
